package saveSetStats;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Arrays;
import java.util.Hashtable;

import com.herohead.Email;

public class saveSetStats {

	static Hashtable<String,Integer> recCountType = new Hashtable<String,Integer>();
	static Hashtable<String,Integer> ssIDs = new Hashtable<String,Integer>();

	static String client;
	static String pool;
	static String volume;
	static String saveDate;
	static Integer ssidSize;
	static String ssid;



	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub


		//String tt = "ch00sm33.eu.mt.mtnet CommonFS BOOST CLONE ~1390942277~ch00sm30_FileSys3_CLONE.CommonFSBOOSTCLONE.001 10/24/2012 2732025580 2189929913";

		//String s1 = cleanupString(tt);
		//System.out.println(s1);
		//System.exit(0);
		String inputFile = "savesetStatus.txt";

		BufferedReader br = new BufferedReader(new FileReader(inputFile));
		try {
			String line = br.readLine();

			while (line != null) {
				//line = line + "\n" + inputFile;

				String s1 = cleanupString(line);

				String[] sss = s1.split(" ");

				client = sss[0];
				pool = sss[1];
				volume = sss[2];
				saveDate = sss[3];
				ssidSize = convertToInt(sss[4]);
				ssid = sss[5];

				if  (volume.startsWith("~")) {

				} else {
					System.out.println(line);
					updateCounts();
				}
				line = br.readLine();
			}

		} finally {
			br.close();
		}

		
		String emailString = "";
		
	       String[] keys = (String[]) recCountType.keySet().toArray(new String[0]);  
	        Arrays.sort(keys);  
	        for(String key : keys) {  
	        	emailString = emailString + key + " : " + recCountType.get(key) + "\n";
	        	//System.out.println(key + " : " + recCountType.get(key));  
	        }  
	        
	   /**     
		       keys = (String[]) ssIDs.keySet().toArray(new String[0]);  
		        Arrays.sort(keys);  
		        for(String key : keys) {  
		        	emailString = emailString + key + " : " + ssIDs.get(key) + "\n";
		        	//System.out.println(key + " : " + recCountType.get(key));  
		        } 
	   **/    
	    
	/**		for (String key : ssIDs.keySet()) {

				String s = key.split(":")[0] + " " +key.split(":")[1] ;
				String ss = Integer.toString(ssIDs.get(key));
				System.out.println(s + " " + ss);
			}
	   **/     
	        
/**		

		for (String key : recCountType.keySet()) {

			String s = key.split(":")[0] + " " +key.split(":")[1] ;
			String ss = Integer.toString(recCountType.get(key));
			System.out.println(s + " " + ss);
		}
**/
	
		    Email em = new Email(emailString);
		    em.addToAddress("craig.david@mt.com");
		    em.setHost("smtp.am.mt.mtnet");
		    em.sendMail("SSID info", emailString);
		    
		        
	        System.out.println(emailString);
	        
	        
	        
	}

	public static String cleanupString(String s) {

		String ss = null;
		String ssid;
		String ssidsize;
		String saveDate;
		String volume;
		String client;
		String temp;

		//System.out.println(s);

		ss = s.replace(" ", "!");
		ss = ss.replace("!!!!!!!!!!!", " ");
		ss = ss.replace("!!!!!!!!!!", " ");
		ss = ss.replace("!!!!!!!!!", " ");
		ss = ss.replace("!!!!!!!!", " ");
		ss = ss.replace("!!!!!!!", " ");
		ss = ss.replace("!!!!!!", " ");
		ss = ss.replace("!!!!!", " ");
		ss = ss.replace("!!!!!", " ");
		ss = ss.replace("!!!!", " ");
		ss = ss.replace("!!!", " ");
		ss = ss.replace("!!", " ");
		ss = ss.replace("!", " ");

		if (ss.split(" ").length == 6) {
			return ss;
		} else {
			String[] s1 = ss.split(" ");
			ssid = s1[s1.length-1];
			ssidsize = s1[s1.length-2];
			saveDate = s1[s1.length-3];
			volume = s1[s1.length-4];
			int i = s1.length -4;
			client = s1[0];
			temp = "";
			

			int ii = 1;

			while (i != ii) {
				temp = temp + s1[ii];
				ii = ii +1;
			}



		}



		return client + " " + temp + " " + volume + " " + saveDate + " " + ssidsize + " " + ssid;
	}


	public static void updateCounts() {

		//update Pool
		//addToCount(recCountType,"Pool:" + pool);
		//addToCount(recCountType,"Pool:" + pool + "_size",ssidSize);
		//update Volume
		addToCount(recCountType,"Volume:" + volume);
		addToCount(recCountType,"Volume:" + volume + "_size",ssidSize);
		// update client
		//addToCount(recCountType,"Client:" + client);
		//addToCount(recCountType,"Client:" + client + "_size",ssidSize);
		// update Total Size
		addToCount(recCountType,"Total: ");
		addToCount(recCountType,"Total: size",ssidSize);

		// update Date
		//addToCount(recCountType,"Date:" + saveDate);
		//addToCount(recCountType,"Date:" + saveDate + "_size",ssidSize);

		// update month
		String ss = saveDate.split("/")[2] + "/" + saveDate.split("/")[0];

		addToCount(recCountType,"Month:" + ss);
		addToCount(recCountType,"Month:" + ss + "_size",ssidSize);
		addToCount(ssIDs,"Month_SSID " + ss + " :" + ssid);
		



	}

	public static int addToCount(Hashtable<String,Integer> myMap,String type) {

		int t = 0;

		if (myMap.containsKey(type)) {
			t = myMap.get(type) +1;
		} else {
			t =1;
		}

		myMap.put(type, t);

		return t;
	}

	public static void addToCount(Hashtable<String,Integer> myMap,String type,Integer i) {

		Integer t = 0;

		if (myMap.containsKey(type)) {
			t = myMap.get(type) + i;
		} else {
			t =i;
		}

		myMap.put(type, t);

		//return t;
	}

	public static Integer convertToInt(String s) {

		Double d = Double.parseDouble(s);
		d = d /1024/1024;

		return d.intValue();

	}

}
